package com.heima.leetcode.practice;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 91. 解码方法
 * @date 2025/5/18 13:22
 */
public class E91 {
    public int numDecoding(String s) {
        char[] charArray = s.toCharArray();
        int[] dp = new int[charArray.length + 1];
        for (int i = 1; i <= charArray.length; i++) {
            dp[i] = charArray[i - 1] == '0' ? 0 : 1;
        }
        dp[0] = 1;
        for (int i = 2; i <= charArray.length; i++) {
            String str = new String(charArray, i - 2, 2);
            int num = Integer.parseInt(str);
            int one = dp[i - 1] * dp[i];
            if (num > 0 && num <= 26 && !str.startsWith("0"))
                dp[i] = one + dp[i - 2];
            else
                dp[i] = one;
        }
        return dp[charArray.length];
    }
}
